package com.atguigu.eduservice.controller.front;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.Result;
import com.atguigu.commonutils.entity.CourseWebOrder;
import com.atguigu.eduservice.client.OrderClient;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.chapter.VideoVo;
import com.atguigu.eduservice.entity.frontvo.CourseFrontVo;
import com.atguigu.eduservice.entity.frontvo.CourseWebVo;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/eduservice/frontCourse")
public class CourseFrontController {
    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private EduVideoService videoService;

    @Autowired
    private OrderClient orderClient;


    //带条件的分页查询
    @PostMapping("pageCourseCondition/{page}/{limit}")
    public Result pageCourseCondition(@PathVariable long page, @PathVariable long limit,
                                      @RequestBody(required = false) CourseFrontVo courseFrontVo){//required=false代表可以为null
        Page<EduCourse> coursePage=new Page<>(page,limit);
        //根据条件查询
        Map<String,Object> map= courseService.selectFrontCourseList(coursePage,courseFrontVo);
        return Result.ok().data(map);
    }

    //查询课程信息
    @GetMapping("getCourseWebInfo/{courseId}")
    public Result getCourseWebInfo(@PathVariable String courseId, HttpServletRequest request){
        //查询课程信息
        CourseWebVo courseWebVo=courseService.getFrontCourseInfo(courseId);
        //查询小结的信息
        List<ChapterVo> chapterList = chapterService.getChapterVideoByCourseId(courseId);

        //查询课程是否购买,通过课程id和用户id来查询status
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        boolean buyCourse = orderClient.isBuyCourse(courseId, memberId);
        return Result.ok().data("courseFrontInfo",courseWebVo).data("chapterList",chapterList).data("isBuy",buyCourse);
    }

    //获取订单中的课程信息
    @PostMapping("getCourseInfoOrder/{id}")
    public CourseWebOrder getCourseInfoOrder(@PathVariable String id){
        CourseWebVo courseInfo = courseService.getFrontCourseInfo(id);
        CourseWebOrder courseWebOrder=new CourseWebOrder();
        BeanUtils.copyProperties(courseInfo,courseWebOrder);
        return courseWebOrder;
    }
}
